package com.ficv.tucanalmovil.dominio;

import java.util.Date;
import java.util.List;

import javax.persistence.EntityManager;
import javax.persistence.OneToOne;
import javax.persistence.Temporal;
import javax.persistence.TemporalType;
import javax.persistence.TypedQuery;

import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.roo.addon.javabean.RooJavaBean;
import org.springframework.roo.addon.jpa.activerecord.RooJpaActiveRecord;
import org.springframework.roo.addon.tostring.RooToString;

@RooJavaBean
@RooToString
@RooJpaActiveRecord(finders = { "findPeticionesesByDestinatarioAndRespuestaIsNotNull", "findPeticionesesByDestinatarioAndRespuestaIsNullAndHoraGreaterThan", "findPeticionesesByDestinatarioAndRemiteAndIdGreaterThan", "findPeticionesesByDestinatarioAndRemiteAndHoraGreaterThan", "findPeticionesesByDestinatarioAndHoraGreaterThan", "findPeticionesesByDestinatarioAndIdGreaterThan", "findPeticionesesByDestinatarioAndRespuestaIsNullAndConfirmar" })
public class Peticiones {

    @Temporal(TemporalType.TIMESTAMP)
    @DateTimeFormat(style = "M-")
    private Date hora;

    private String texto;

    @Temporal(TemporalType.TIMESTAMP)
    @DateTimeFormat(style = "M-")
    private Date respuesta;

    private Boolean confirmar;

    @OneToOne
    private People remite;

    @OneToOne
    private People destinatario;

    @Temporal(TemporalType.TIMESTAMP)
    @DateTimeFormat(style = "M-")
    private Date recibido;

    public static List<Peticiones> findChat(People destinatario, People remite, Date hora) {
        if (destinatario == null) throw new IllegalArgumentException("The destinatario argument is required");
        if (remite == null) throw new IllegalArgumentException("The remite argument is required");
        if (hora == null) throw new IllegalArgumentException("The hora argument is required");
        EntityManager em = Peticiones.entityManager();
        String query = "SELECT o FROM Peticiones AS o WHERE (o.destinatario = :destinatario AND o.remite = :remite OR o.destinatario = :remite AND o.remite = :destinatario)  ORDER BY o.id";
      
        TypedQuery<Peticiones> q = em.createQuery(query, Peticiones.class);
       
        q.setParameter("destinatario", destinatario);
        q.setParameter("remite", remite);
        //q.setParameter("hora", hora);
       
        
        
        int count=q.getResultList().size();
        int first=count>10?count-10:0;
        
        q=em.createQuery(query, Peticiones.class);
        
        q.setParameter("destinatario", destinatario);
        q.setParameter("remite", remite);
        //q.setParameter("hora", hora);
        
        return q.setFirstResult(first).setMaxResults(10).getResultList();
        
        
//        return q;
    }

    public static TypedQuery<com.ficv.tucanalmovil.dominio.Peticiones> findChat(People destinatario, People remite, Long id) {
        if (destinatario == null) throw new IllegalArgumentException("The destinatario argument is required");
        if (remite == null) throw new IllegalArgumentException("The remite argument is required");
        if (id == null) throw new IllegalArgumentException("The id argument is required");
        EntityManager em = Peticiones.entityManager();
        TypedQuery<Peticiones> q = em.createQuery("SELECT o FROM Peticiones AS o WHERE (o.destinatario = :destinatario AND o.remite = :remite OR o.destinatario = :remite AND o.remite = :destinatario) AND o.id > :id ORDER BY o.id", Peticiones.class);
        q.setParameter("destinatario", destinatario);
        q.setParameter("remite", remite);
        q.setParameter("id", id);
        return q;
    }

    public static List<com.ficv.tucanalmovil.dominio.Peticiones> findChat(People destinatario, Date hora) {
        if (destinatario == null) throw new IllegalArgumentException("The destinatario argument is required");
        if (hora == null) throw new IllegalArgumentException("The hora argument is required");
        EntityManager em = Peticiones.entityManager();
        String query="SELECT o FROM Peticiones AS o WHERE o.destinatario = :destinatario  ORDER BY o.id";
        
        TypedQuery<Peticiones> q = em.createQuery(query, Peticiones.class);
        
        q.setParameter("destinatario", destinatario);

        //q.setParameter("hora", hora);
       
        
        
        int count=q.getResultList().size();
        int first=count>10?count-10:0;
        
        q=em.createQuery(query, Peticiones.class);
        
        q.setParameter("destinatario", destinatario);
        //q.setParameter("hora", hora);
        
        return q.setFirstResult(first).setMaxResults(10).getResultList();
        

    }

    public static TypedQuery<com.ficv.tucanalmovil.dominio.Peticiones> findChat(People destinatario, Long id) {
        if (destinatario == null) throw new IllegalArgumentException("The destinatario argument is required");
        if (id == null) throw new IllegalArgumentException("The id argument is required");
        EntityManager em = Peticiones.entityManager();
        TypedQuery<Peticiones> q = em.createQuery("SELECT o FROM Peticiones AS o WHERE o.destinatario = :destinatario  AND  o.id > :id ORDER BY o.id", Peticiones.class);
        q.setParameter("destinatario", destinatario);
        q.setParameter("id", id);
        return q;
    }
}
